package com.gxa.day2022_08_09_28.emp_dept_system.ui.frame;



import com.gxa.day2022_08_09_28.emp_dept_system.entity.Dept;
import com.gxa.day2022_08_09_28.emp_dept_system.entity.Emp;
import com.gxa.day2022_08_09_28.emp_dept_system.service.EmpService;
import com.gxa.day2022_08_09_28.emp_dept_system.service.impl.EmpServiceImpl;
import com.gxa.day2022_08_09_28.emp_dept_system.ui.tablemodel.EmpTableModel;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

public class UpdateFrame {
    private Emp emp;
    public UpdateFrame(Emp emp){
        this.emp = emp;
    }
    public void launchFrame(){
        JFrame mainFrame = new JFrame("修改界面");
        mainFrame.setLayout(null);
        mainFrame.setSize(600,700);
        mainFrame.setLocationRelativeTo(null);
        mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        addWidget2Frame(mainFrame);

        mainFrame.setVisible(true);

    }

    public void addWidget2Frame(JFrame frame){

        JLabel empnoLable = new JLabel("雇员编号");
        empnoLable.setBounds(100,100,100,20);
        JLabel enameLable = new JLabel("雇员姓名");
        enameLable.setBounds(100,140,100,20);
        JLabel jobLabel = new JLabel("工种");
        jobLabel.setBounds(100,180,100,20);
        JLabel deptLabel = new JLabel("部门");
        deptLabel.setBounds(100,220,100,20);


        JTextField empnoField = new JTextField();
        empnoField.setBounds(210,100,100,20);
        empnoField.setText(emp.getEmpno()+"");
        empnoField.setEnabled(false);//不能修改

        JTextField enameField = new JTextField();
        enameField.setBounds(210,140,100,20);
        enameField.setText(emp.getEname());

        JTextField jobField = new JTextField();
        jobField.setBounds(210,180,100,20);
        jobField.setText(emp.getJob());

        //当前选中的部门
        Dept dept = emp.getDept();

        EmpService empService = new EmpServiceImpl();
        Vector<Dept> deptData = empService.queryDepts();//获取所有的部门

        JComboBox deptComboBox = new JComboBox(deptData);
        deptComboBox.setBounds(210,220,100,20);
        deptComboBox.setSelectedItem(dept);


        JButton saveButton = new JButton("保存");
         saveButton.setBounds(280,260,80,20);

         saveButton.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                 //收集数据
                 String empno = empnoField.getText();
                 String ename = enameField.getText();
                 String job = jobField.getText();
                 Dept dept = (Dept) deptComboBox.getSelectedItem();

                 //封装成对象
                 Emp emp = new Emp(Integer.valueOf(empno),ename,job);
                 emp.setDept(dept);
                 EmpService empService = new EmpServiceImpl();
                 empService.update(emp);


                 //刷新表格
                 //1、拿到表格
                 //2、 重建里面的数据
                 //获取最新数据
                 Object[][] emps = empService.queryAll();
                 EmpTableModel empTableModel = EmpTableModel.getInstance();
                 empTableModel.setEmps(emps);
                 MainFrame.getEmpTable().updateUI();

             }
         });

        frame.add(deptLabel);
        frame.add(deptComboBox);
        frame.add(saveButton);
        frame.add(empnoField);
        frame.add(enameField);
        frame.add(jobField);
        frame.add(empnoLable);
        frame.add(enameLable);
        frame.add(jobLabel);
    }
}
